package com.hzspace.advice.controller;

import com.hzspace.advice.bean.Author;
import com.hzspace.advice.bean.Book;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author: LiuHao
 * @Descirption:
 * @Date: 2020/2/28_21:15
 */
@RestController
public class HelloController {

    /**
     * ControllerAdvice的三种用法
     * 1. 统一异常处理
     * 2. 预设全局数据
     * 3. 请求参数预处理
     */

    @GetMapping("/exception")
    public Object exception(){
        throw new RuntimeException("自定义异常");
    }

    @GetMapping("/map")
    public Object map(Model model){
        System.out.println(model);//{info={address=www.hzspace.top, name=LH}}
        return "hello";
    }

    /**
     * 请求参数预处理 区分book与author里name的不同
     * @param book
     * @param author
     */
    @PostMapping("preProcess")
    public void preProcess(@ModelAttribute(value = "book") Book book, @ModelAttribute("author") Author author){
        System.out.println(book);
        System.out.println(author);
    }
}
